package com.gx.smart.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewbinding.ViewBinding;


/**
 * 基础fragment
 */

public abstract class BaseMVVMFragment<T extends ViewBinding, VM extends BaseViewModel> extends BaseFragment {

    protected VM viewModel;
    protected T binding;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getActivity().getApplication())).get(onBindViewModel());
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = onBindViewBinding(inflater, container, false);
        View view = binding.getRoot();
        initView(view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        initContent();
        initData();
        initObserver();
    }

    @Deprecated
    @Override
    protected int onBindLayout() {
        return 0;
    }

    protected abstract Class<VM> onBindViewModel();

    protected abstract T onBindViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup parent, boolean attachToParent);

    /**
     * 初始化view
     *
     * @param view
     */
    @Override
    protected void initView(@NonNull View view) {

    }

    /**
     * 初始化内容
     */
    @Override
    protected void initContent() {

    }

    /**
     * 网络请求数据
     */
    @Override
    protected void initData() {

    }

    /**
     * 获取viewModel中的数据
     */
    protected void initObserver() {
    }

}
